package com.neusoft.neuiotms.dcom.service.change.impl;

import java.util.List;

import javax.persistence.EntityNotFoundException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.neusoft.bizcore.web.constant.ExceptionConstants;
import com.neusoft.bizcore.web.exception.UnitedException;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.converter.change.ChangeCiPropertyConverter;
import com.neusoft.neuiotms.dcom.dto.change.ChangeCiPropertyDTO;
import com.neusoft.neuiotms.dcom.model.change.ChangeCiProperty;
import com.neusoft.neuiotms.dcom.repository.change.ChangeCiPropertyRepository;
import com.neusoft.neuiotms.dcom.service.change.ChangeCiPropertyService;

@Service
public class ChangeCiPropertyServiceImpl implements ChangeCiPropertyService {

    @Autowired
    private ChangeCiPropertyRepository changeCiPropertyRepository;

    @Autowired
    private ChangeCiPropertyConverter changeCiPropertyConverter;

    @Override
    public List<ChangeCiPropertyDTO> index(final Searchable searchable) {
        final List<ChangeCiProperty> models = this.changeCiPropertyRepository.index(searchable);
        return this.changeCiPropertyConverter.toListDTO(models);
    }

    @Override
    public Page<ChangeCiPropertyDTO> search(final Pageable pageable, final Searchable searchable) {
        final Page<ChangeCiProperty> result = this.changeCiPropertyRepository.search(pageable, searchable);
        return this.changeCiPropertyConverter.toPageDTO(result);
    }

    @Override
    public ChangeCiPropertyDTO create(final ChangeCiPropertyDTO dto) {
        final ChangeCiProperty model = this.changeCiPropertyConverter.toModel(dto);
        this.changeCiPropertyRepository.save(model);
        return this.changeCiPropertyConverter.toDTO(model);
    }

    @Override
    public ChangeCiPropertyDTO update(final Long id, final ChangeCiPropertyDTO dto) {
        ChangeCiProperty model;
        try {
            model = this.changeCiPropertyRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }

        dto.setId(model.getId());
        model = this.changeCiPropertyConverter.toModel(dto);
        this.changeCiPropertyRepository.save(model);

        return this.changeCiPropertyConverter.toDTO(model);
    }

    @Override
    public ChangeCiPropertyDTO details(final Long id) {
        final ChangeCiProperty model;
        try {
            model = this.changeCiPropertyRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }

        return this.changeCiPropertyConverter.toDTO(model);
    }

    @Override
    public void delete(final Long id) {
        try {
            this.changeCiPropertyRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }

        this.changeCiPropertyRepository.deleteById(id);
    }

}
